package com.apps.ftapp.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.apps.ftapp.R;
import com.apps.ftapp.model.BookModel;
import com.apps.ftapp.model.OrderModel;
import com.apps.ftapp.util.DateUtil;
import com.apps.ftapp.util.Tools;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import org.w3c.dom.Text;

import java.util.Date;
import java.util.List;

/**
 * Created by lyli on 2017/10/8.
 */

public class MyOrderAdapter extends BaseAdapter {

    private Context context;
    private List<OrderModel.DataBean> dataList;
    private LayoutInflater layoutInflater;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions options;


    public MyOrderAdapter(Context context, List<OrderModel.DataBean> dataList) {
        this.context = context;
        this.dataList = dataList;
        layoutInflater = LayoutInflater.from(this.context);

        //显示图片的配置  
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher)
                .showImageOnFail(R.mipmap.ic_launcher).cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565).build();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));

    }


    public void addRes(List<OrderModel.DataBean> data) {
        if (data != null) {
//            this.dataList.clear();
            this.dataList.addAll(data);
            notifyDataSetChanged();
        }
    }


    @Override
    public int getCount() {
        if (null != dataList && dataList.size() > 0) {
            return dataList.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        OrderModel.DataBean bean = dataList.get(i);
        ViewHolder viewHolder;
        if (view == null) {
            view = this.layoutInflater.inflate(R.layout.activity_my_order_listview_item, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        viewHolder.order_title.setText(bean.getTitle());
        viewHolder.orderNo.setText("订单编号：" + bean.getSn());
        viewHolder.orderStatus.setText(bean.getStatus());
        viewHolder.orderStatus.setText(Tools.getSt(bean.getStatus()));
        viewHolder.orderPrice.setText("¥" + bean.getPrice());
        viewHolder.orderDesc.setText("已购" + bean.getStudentNum() + "/限购" + bean.getMaxStudentNum());
        viewHolder.orderTime.setText(DateUtil.timeStamp2Date(bean.getCreatedTime(), "yyyy-MM-dd hh:mm:ss"));
        imageLoader.displayImage(bean.getLargePicture(), viewHolder.imageView, options);

        viewHolder.orderPayMoney.setText("实付:" + bean.getTotalPrice());

        return view;
    }


    class ViewHolder {
        TextView orderNo;
        TextView orderStatus;
        TextView orderDesc;
        TextView orderPrice;
        TextView orderPayMoney;
        TextView orderWuliu;
        TextView orderTime;
        ImageView imageView;
        TextView order_title;

        public ViewHolder(View view) {
            orderNo = view.findViewById(R.id.order_no);
            orderStatus = view.findViewById(R.id.order_status);
            orderPrice = view.findViewById(R.id.order_price);
            orderDesc = view.findViewById(R.id.order_desc);
            orderWuliu = view.findViewById(R.id.order_wuliu);
            orderPayMoney = view.findViewById(R.id.order_pay_money);
            orderTime = view.findViewById(R.id.order_time);
            imageView = view.findViewById(R.id.order_img);
            order_title = view.findViewById(R.id.order_title);


        }
    }
}
